﻿/*


【参考资料】
Show popup window with faded background window WPF (原文比较啰嗦)
https://www.codeproject.com/Questions/1274727/Show-popup-window-with-faded-background-window-WPF 
*/

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

namespace VerficationsWPF.Verifications
{
    /// <summary>
    /// FontTestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FontTestWindow : Window
    {
        FontFamily lastFontFamily = new FontFamily("HYSWLongFangSong.ttf");

        public FontTestWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
        }

        public void ShowWindow(Window popupWindow, Window owner)
        {
            if (popupWindow != null && owner != null)
                this.Dispatcher.Invoke(() =>
                {
                    owner.Opacity = 0.6;
                    owner.Effect = new System.Windows.Media.Effects.BlurEffect();
                    popupWindow.ShowInTaskbar = false;
                    popupWindow.Owner = owner;
                    popupWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    if (this.WindowState != WindowState.Maximized)
                    {
                        popupWindow.Left = Application.Current.MainWindow.Left + (this.ActualWidth - popupWindow.ActualWidth) / 2;
                        popupWindow.Top = Application.Current.MainWindow.Top + (this.ActualHeight - popupWindow.ActualHeight) / 2;
                    }
                    popupWindow.ShowDialog();
                });
        }


        public void Progress_Close(Window popupWindow, Window parentWindow)
        {
            if (popupWindow != null && parentWindow != null)
                this.Dispatcher.Invoke(() =>
            {
                parentWindow.Opacity = 1;
                parentWindow.Effect = null;
                if (popupWindow.IsVisible && popupWindow.IsVisible)
                    popupWindow.Hide();
            });
        }

        private void btnDynamicFont_Click(object sender, RoutedEventArgs e)
        {
            var temp = this.FontFamily;
            this.FontFamily = lastFontFamily;
            lastFontFamily = temp;

            txtOutput.Text += $"Current FontFamily: {this.FontFamily}\n";
        }

        private void btnOpenDialog_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Dispatcher.Invoke(() =>
                {
                    var window = new FontTestWindow();
                    window.Width -= 240;
                    window.Height -= 140;
                    window.Closing += (e, s) => Progress_Close(window, this);
                    ShowWindow(window, this);
                });
            }
            catch (Exception ex)
            {
                Debug.WriteLine("MainUserControl.xaml.cs|TabItem_MouseUp(): " + ex.Message);
            }
        }

        public void ShowFadedWindow(Window window)
        {
            this.Dispatcher.Invoke(() =>
            {
                window.Closing += (e, s) => Progress_Close(window, this);
                ShowWindow(window, this);
            });
        }

        private void btnShowDialog_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                var window = new RoundCornerWindow(); 
                window.Closing += (e, s) => Progress_Close(window, this);
                ShowWindow(window, this);
            });
        }

        private void btnColors_Click(object sender, RoutedEventArgs e)
        {
            // ShowFadedWindow(new ColorWindow());
        }

        private void btnButifulButton_Click(object sender, RoutedEventArgs e)
        {
            ShowFadedWindow(new ButifulDialog());
        }
    }
}
